define(['jquery'], function ($) {

    function mousemove() {
        var swipe = {
            option: {
                width: 0,
                parent_width: 0,
                left: 0,
                move_num: 0,
                max_move: 0,
            },
            obj: {}, //移动的对象

            touch_option: {
                direction: 'no', // left  right  up  down
                start_x: 0, //开始的X坐标
                start_y: 0, //开始的X坐标
                end_x: 0, //结束的X坐标
                end_y: 0, //结束的X坐标
                x_length: 0, //横向移动距离
                y_length: 0, //纵向移动距离
            },


            //开始执行
            run: function (el) {
                var obj = $(el);
                this.obj = obj; //获取幻灯片实例
                var parent = obj.eq(0).parent();
                this.option.parent_width = parent[0].offsetWhith;
                var li = obj.find(">li");

                var ul_w = 0;
                for (var i = 0; i < li.length; i++) {
                    if (i == li.length - 1) {
                        ul_w += parseInt(li.eq(i).css('width'))
                    } else {
                        ul_w += parseInt(li.eq(i).css('margin-right')) + parseInt(li.eq(i).css('width'))
                    }

                }
                this.option.width = ul_w;
                //监听touch 事件
                this.setTouch(); //监听幻灯片触屏事件

                //执行动画
                //this.goSwipe();
            },



            //执行切换
            goSwipe: function () {
                if (this.option.auto_swipe) {
                    var that = this;
                    setInterval(function () {
                        that.moveSwipe();
                    }, this.option.wait_time)
                }
            },
            //移动幻灯片
            moveSwipe: function () {
                //计算移动距离
                var num = (this.option.curr_swipe + 1) * this.option.width;
                if (this.option.curr_swipe == this.option.swipe_num - 1) {
                    num = 0;
                }
                //判断是否为最后一张幻灯片
                if (this.option.curr_swipe == this.option.swipe_num - 1) {
                    this.option.curr_swipe = 0;
                } else {
                    this.option.curr_swipe++;
                }

                this.obj.find("ul").animate({ left: '-' + num + 'px' });

                //console.log(this.option.curr_swipe); //切换完了
                //切换小圆点
                this.changeDot();

            },



            //监听幻灯片触屏事件
            setTouch: function () {
                var that = this;
                //监听开始触摸屏幕时的事件
                this.obj.on('touchstart', function (event) {
                    document.addEventListener("touchmove", that.fn2, {
                        passive: false
                    });
                    //console.log(event);
                    //重新初始化
                    that.touch_option = {
                        direction: 'no', // left  right  up  down
                        start_x: 0, //开始的X坐标
                        start_y: 0, //开始的X坐标
                        end_x: 0, //结束的X坐标
                        end_y: 0, //结束的X坐标
                        x_length: 0, //横向移动距离
                        y_length: 0, //纵向移动距离
                    };
                    var tmp = event.originalEvent.targetTouches[0]; //事件坐标
                    that.touch_option.start_x = tmp.pageX; //取得开始 x 坐标
                    that.touch_option.start_y = tmp.pageY; //取得开始 y 坐标
                    //console.log(that.touch_option);
                });
                //监听移动触摸屏幕时的事件
                this.obj.on('touchmove', function (event) {
                    //console.log(event);
                });
                //监听移动触摸屏幕时的事件
                this.obj.on('touchend', function (event) {
                    //console.log(event);
                    var tmp = event.originalEvent.changedTouches[0]; //事件坐标
                    that.touch_option.end_x = tmp.pageX; //取得结束 x 坐标
                    that.touch_option.end_y = tmp.pageY; //取得结束 y 坐标

                    //判断移动距离,
                    that.touch_option.x_length = Math.abs(that.touch_option.end_x - that.touch_option.start_x); //横向移动距离
                    that.touch_option.y_length = Math.abs(that.touch_option.end_y - that.touch_option.start_y); //纵向移动距离

                    //判断移动方向
                    if (that.touch_option.x_length - that.touch_option.y_length > 0) {
                        //左右移动
                        if (that.touch_option.x_length - that.touch_option.y_length == 0) {
                            //没有移动就啥不干
                            return;
                        }
                        if (that.touch_option.start_x - that.touch_option.end_x > 0) {
                            that.touch_option.direction = 'left'; //向左划屏
                        } else {
                            that.touch_option.direction = 'right'; //向右划屏
                        }
                        that.option.move_num = that.touch_option.x_length;
                    } else {
                        //上下移动
                        if (that.touch_option.start_y - that.touch_option.end_y == 0) {
                            //没有移动就啥不干
                            return;
                        }
                        if (that.touch_option.start_y - that.touch_option.end_y > 0) {
                            that.touch_option.direction = 'up'; //向上划屏
                        } else {
                            that.touch_option.direction = 'down'; //向下划屏
                        }
                        that.option.move_num = that.touch_option.y_length;
                    }
                    //console.log(that.touch_option.direction);
                    that.manualMove(); //移动幻灯片
                    document.removeEventListener('touchmove', that.fn2);
                });
            },

            fn2: function (e) {
                e.preventDefault && e.preventDefault();
            },


            manualMove: function () {
                var direction = this.touch_option.direction; //获取方向

                if (this.option.width <= this.option.parent_width) {
                    return;
                } else {
                    var max_move = Math.abs(this.option.parent_width - this.option.width); //移动的最大距离
                    this.option.max_move = max_move;

                    //计算移动距离 
                    if (this.option.move_num > max_move) {
                        this.option.move_num = max_move
                    }


                    if (direction == 'left' || direction == 'right') {
                        if (direction == 'left') {

                            this.option.left = this.option.left + this.option.move_num;
                            if (this.option.left >= max_move) {
                                this.option.left = max_move;
                            }
                            this.obj.animate({ left: '-' + this.option.left + 'px' }); //移动动画
                        } else {
                            //console.log('right');
                            this.option.left = this.option.left - this.option.move_num;
                            if (this.option.left <= 0) {
                                this.option.left = 0;
                            }
                            this.obj.animate({ left: '-' + this.option.left + 'px' }); //移动动画
                        }
                        //console.log('left', this.option);
                    }
                }
            },


        }
        return swipe;
    }

    return {
        move: mousemove
    }
});